package com.it.product.comtroller;

import com.spzx.product.api.domain.Brand;

import com.it.product.service.IBrandService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * projectName: spzx-parent
 *
 * @author: 王宝贵
 * time: 2024/9/19 18:49 周四
 * description:
 */
@RestController
@Tag(name = "品牌接口管理")
@RequestMapping("/brand")
public class BrandController  extends BaseController {

    @Autowired
    IBrandService brandService;

    /**
     * 查询全部品牌
     * @param brand
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "查询全部的品牌")
    public TableDataInfo list(Brand brand){
        //若依框架自己封装好的分页
        startPage();
        List<Brand> brandList = brandService.list(brand);
        return getDataTable(brandList);
    }

    /**
     * 查询品牌
     * @param id  主键id
     * @return
     */
    @Operation(summary = "根据id获取详细信息")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id){
        Brand brand = brandService.selectById(id);
        return success(brand);
    }

    /**
     * 新增品牌
     * @param brand 品牌信息
     * @return
     */
    @Operation(summary = "新增品牌")
    @PostMapping
    public AjaxResult save(@Validated @RequestBody Brand brand){
        //  TODO 获取当前操作人的名字
        brand.setCreateBy(SecurityUtils.getUsername());
        long rows= brandService.save(brand);
        return AjaxResult.success(rows);
    }

    /**
     * 修改品牌
     * @param brand 品牌信息
     * @return
     */
    @Operation(summary="修改品牌")
    @PutMapping
    public AjaxResult updateBrand(@Validated @RequestBody Brand brand){
        brand.setUpdateBy(SecurityUtils.getUsername());
        Long rows= brandService.updateBrand(brand);
        return AjaxResult.success(rows);
    }

    @Operation(summary="删除品牌")
    @PutMapping("/{ids}")
    public AjaxResult removeBrand(@PathVariable("ids") Long[] ids){
        Long rows= brandService.deleteBrandByIds(ids);
        return toAjax(rows);
    }










    // =================================以下接口是远程调用的接口=================================

    /**
     * 查询所有品牌信息
     * @return 返回集合 品牌 多个
     */
    @InnerAuth
    @Operation(summary = "查询所有品牌")
    @GetMapping("getBrandAllList")
    public R<List<Brand>> getBrandAllList(){
        List<Brand> list = brandService.list(null);
        return R.ok(list);
    }





}